package tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws Exception {
        //创建ServerSocket对象，指定端口
        ServerSocket server = new ServerSocket(7890);
        System.out.println("服务器端开启");
        //响应客户端发送的请求
        Socket socket = server.accept();
        //获取传输数据的流对象
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();

        //传输数据
        byte[] bytes = new byte[1024];
        int len = is.read(bytes);
        String msg = new String(bytes,0,len);
        String ip = socket.getInetAddress().getHostAddress();
        System.out.println(ip+"发送了"+msg);

        os.write("我嫩蝶！".getBytes());
        //关流释放资源
        socket.close();
        server.close();

    }
}
